class ArgumentsController < ApplicationController
  before_filter :approval_required, :only => [ :new, :create, :edit, :update ]

  # GET /arguments
  # GET /arguments.xml
  def index
    @arguments = Argument.find(:all)
  end

  # GET /arguments/1
  # GET /arguments/1.xml
  def show
    @argument = Argument.find(params[:id])
  end

  # GET /arguments/new
  # GET /arguments/new.xml
  def new
    @argument = Argument.new
  end

  # GET /arguments/1/edit
  def edit
    @argument = Argument.find(params[:id])
  end

  # POST /arguments
  # POST /arguments.xml
  def create
    @argument = Argument.new(params[:argument])

    if @argument.save
      flash[:notice] = 'Argument was successfully created.'.t
      redirect_to :controller => "bingo", :action => "index", :start_position => BingoController::CENTER_SQUARE_POSITION, :new_entry => [@argument.id] 
    else
      flash[:error] = 'Argument could not be created!'.t
      render :action => "new"
    end
  end

  # PUT /arguments/1
  # PUT /arguments/1.xml
  def update
    # only allows you to edit your own records    
    @argument = Argument.find(params[:id])
    if !@argument 
      flash[:error] = "Could not find argument with given id.".t
    elsif @argument.user != current_user
      flash[:error] = "You may only edit arguments you own.".t
    elsif !@argument.update_attributes(params[:argument])
      flash[:error] = "Problem updating argument.".t
    else   
      flash[:notice] = "Argument was successfully updated.".t
    end
    
    redirect_to(arguments_url)
  end
  
  # DELETE /arguments/1
  # DELETE /arguments/1.xml
  def destroy
    @argument = Argument.find(params[:id])
    
    if !@argument 
      flash[:error] = "Could not find argument with given id.".t
    elsif !(@argument.user == current_user || current_user.is_admin?)
      flash[:error] = "You may only remove arguments you own.".t
    elsif !@argument.destroy
      flash[:error] = "Problem removing argument.".t
    else   
      flash[:notice] = "Argument was successfully removed.".t
    end
    
    redirect_to(arguments_url)
  end

end
